package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.OrderItem;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class OrderItemService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public OrderItemService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds OrderItem to the db.
     * @param oi OrderItem to be inserted
     * @return ID of the added OrderItem
     */
    public int addOrderItem(OrderItem oi) {
        session.beginTransaction();
        int id = getMaxOrderItemID() + 1;
        oi.setOrderItemID(id);
        id = (Integer) session.save(oi);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of OrderItems.
     * @return Max ID
     */
    public int getMaxOrderItemID() {
        String hql = "select max(o.orderItemID) from OrderItem o";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxoiid = (Integer) list.get(0);
            return maxoiid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a OrderItem identified by the ID.
     * @param id of the OrderItem
     * @return OrderItem with the ID given (null if not found)
     */
    public OrderItem getOrderItemByID(int id) {
        session.beginTransaction();
        OrderItem oi = (OrderItem) session.get(OrderItem.class, new Integer(id));
        session.getTransaction().commit();
        return (oi);
    }

    /**
     * Retrieves a list of all OrderItems stored in the db.
     * @return List of all OrderItems (null if no OrderItems)
     */
    public List<OrderItem> getAllOrderItems() {
        session.beginTransaction();
        List<OrderItem> oiList = session.createQuery("from OrderItem").list();
        session.getTransaction().commit();
        return (oiList);
    }

    /**
     * Updates a specified OrderItem identified by ID.
     * @param newOrderItem an updated OrderItem
     */
    public void updateOrderItem(OrderItem newOrderItem) {
        session.beginTransaction();
        OrderItem oi = (OrderItem) session.get(OrderItem.class, new Integer(newOrderItem.getOrderItemID()));
        oi.setBoxOrder(newOrderItem.getBoxOrder());
        oi.setOfferItem(newOrderItem.getOfferItem());
        oi.setPieces(newOrderItem.getPieces());
        oi.setPrice(newOrderItem.getPrice());
        oi.setVolume(newOrderItem.getVolume());
        oi.setWeight(newOrderItem.getWeight());

        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the OrderItem from db.
     * @param id OrderItem ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteOrderItem(int id) {
        try {
            session.beginTransaction();
            OrderItem oi = (OrderItem) session.get(OrderItem.class, new Integer(id));
            session.delete(oi);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}
